home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
ISSUE09
/
FILES
/
NAMES3U.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-04-09
|
9KB
|
315 lines
unit Names3u;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Menus, Buttons, ExtCtrls, StdCtrls, ClipBrd, Mask, Names3U2;
const
wm_MadeUpMsg = wm_User + 100;
RecordNumStr = '%d of %d';
type
TState = (sInactive, sBrowse, sChange, sAppend);
TDataForm = class(TForm)
MainMenu1: TMainMenu;
FirstMnu: TMenuItem;
PriorMnu: TMenuItem;
NextMnu: TMenuItem;
LastMnu: TMenuItem;
FileMnu: TMenuItem;
EditMnu: TMenuItem;
DeleteMnu: TMenuItem;
PasteMnu: TMenuItem;
CopyMnu: TMenuItem;
CutMnu: TMenuItem;
UndoMnu: TMenuItem;
ExitBtn: TMenuItem;
IgnoreMe2: TMenuItem;
IgnoreMe1: TMenuItem;
SpeedBar: TPanel;
UndoBtn: TSpeedButton;
CutBtn: TSpeedButton;
CopyBtn: TSpeedButton;
PasteBtn: TSpeedButton;
FirstBtn: TSpeedButton;
PriorBtn: TSpeedButton;
NextBtn: TSpeedButton;
LastBtn: TSpeedButton;
SpeedButton1: TSpeedButton;
NameEdt: TEdit;
StatusLine: TPanel;
RecordNum: TPanel;
DOBEdt: TMaskEdit;
IgnoreMe3: TMenuItem;
NewMnu: TMenuItem;
IgnoreMe4: TMenuItem;
ChangeMnu: TMenuItem;
NewBtn: TSpeedButton;
ChangeBtn: TSpeedButton;
DeleteBtn: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure NewClick(Sender: TObject);
procedure ChangeClick(Sender: TObject);
procedure FirstClick(Sender: TObject);
procedure PriorClick(Sender: TObject);
procedure NextClick(Sender: TObject);
procedure LastClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
procedure EditItemClick(Sender: TObject);
procedure EditBoxKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure EditBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure EditBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure EditBoxChange(Sender: TObject);
procedure EditBoxExit(Sender: TObject);
private
{ Defaults to sInactive }
FState: TState;
procedure DoHint(Sender: TObject);
procedure SetupButtons;
procedure SetUpClipBoardItems(Sender: TComponent);
procedure ShowRecord(RecNo: Cardinal);
procedure ShowRecNo(RecNo: Cardinal);
procedure WMMadeUpMsg(var Msg: TMessage); message wm_MadeUpMsg;
procedure SetState(State: TState);
public
DataFile: TDataFile;
DataRec: TDataRec;
property State: TState read FState write SetState;
end;
var
DataForm: TDataForm;
implementation
{$R *.DFM}
procedure TDataForm.DoHint(Sender: TObject);
begin
StatusLine.Caption := Application.Hint;
end;
procedure TDataForm.SetState(State: TState);
begin
if FState <> State then
begin
if FState in [sChange, sAppend] then
begin
DataRec.Name := NameEdt.Text;
DataRec.DOB := StrToDate(DOBEdt.Text);
DataFile.Records[DataFile.Current] := DataRec;
end;
FState := State;
if FState = sAppend then
DataFile.Current := DataFile.Count;
ShowRecNo(DataFile.Current);
SetupButtons;
ChangeMnu.Enabled := FState <> sInactive;
ChangeBtn.Enabled := ChangeMnu.Enabled;
ChangeMnu.Checked := State in [sChange, sAppend];
ChangeBtn.Down := ChangeMnu.Checked;
NameEdt.Enabled := ChangeMnu.Checked;
DOBEdt.Enabled := ChangeMnu.Checked;
end;
end;
procedure TDataForm.SetupButtons;
begin
FirstBtn.Enabled := DataFile.Current > 0;
FirstMnu.Enabled := FirstBtn.Enabled;
PriorBtn.Enabled := FirstBtn.Enabled;
PriorMnu.Enabled := FirstBtn.Enabled;
NextBtn.Enabled := DataFile.Current < Pred(DataFile.Count);
NextMnu.Enabled := NextBtn.Enabled;
LastBtn.Enabled := NextBtn.Enabled;
LastMnu.Enabled := NextBtn.Enabled;
end;
procedure TDataForm.SetUpClipBoardItems(Sender: TComponent);
var
EditCtl: TCustomEdit;
begin
if (Sender is TCustomEdit) and TCustomEdit(Sender).Enabled then
begin
EditCtl := Sender as TCustomEdit;
UndoMnu.Enabled := Bool(EditCtl.Perform(em_CanUndo, 0, 0));
UndoBtn.Enabled := UndoMnu.Enabled;
CutMnu.Enabled := EditCtl.SelLength > 0;
CutBtn.Enabled := CutMnu.Enabled;
CopyMnu.Enabled := CutMnu.Enabled;
CopyBtn.Enabled := CutMnu.Enabled;
PasteMnu.Enabled := ClipBoard.AsText <> '';
PasteBtn.Enabled := PasteMnu.Enabled;
DeleteMnu.Enabled := EditCtl.SelLength > 0;
DeleteBtn.Enabled := DeleteMnu.Enabled;
end
else
begin
UndoMnu.Enabled := False;
UndoBtn.Enabled := False;
CutMnu.Enabled := False;
CutBtn.Enabled := False;
CopyMnu.Enabled := False;
CopyBtn.Enabled := False;
PasteMnu.Enabled := False;
PasteBtn.Enabled := False;
DeleteMnu.Enabled := False;
DeleteBtn.Enabled := False;
end;
end;
procedure TDataForm.ShowRecord(RecNo: Cardinal);
begin
try
ShowRecNo(RecNo);
with DataFile[RecNo] do
begin
NameEdt.Text := Name;
DOBEdt.Text := DateToStr(DOB);
end;
State := sBrowse;
SetupButtons;
except
on EListError do { Nothing }
end;
end;
{ Pass in real record number, and it adds 1 (they start at 0) }
procedure TDataForm.ShowRecNo(RecNo: Cardinal);
begin
if State = sAppend then
RecordNum.Caption := Format(RecordNumStr, [Succ(RecNo), Succ(DataFile.Count)])
else
if DataFile.Count > 0 then
RecordNum.Caption := Format(RecordNumStr, [Succ(RecNo), DataFile.Count])
else
RecordNum.Caption := 'No records';
end;
procedure TDataForm.WMMadeUpMsg(var Msg: TMessage);
begin
SetUpClipBoardItems(ActiveControl);
end;
procedure TDataForm.FormCreate(Sender: TObject);
begin
Application.OnHint := DoHint;
{ When the form has finished setting itself up, (i.e. all }
{ the controls have been made and focus has been given to }
{ one of them) this posted message will be processed and }
{ so the edit buttons/menus will be in a sensible state }
PostMessage(Handle, wm_MadeUpMsg, 0, 0);
DataFile := TDataFile.Create;
NewMnu.Enabled := True;
ShowRecord(0);
end;
procedure TDataForm.FormDestroy(Sender: TObject);
begin
if Assigned(DataFile) then
DataFile.Free;
end;
procedure TDataForm.NewClick(Sender: TObject);
begin
State := sAppend;
NameEdt.SetFocus;
end;
procedure TDataForm.ChangeClick(Sender: TObject);
const
States: array[Boolean] of TState = (sBrowse, sChange);
Hints: array[Boolean] of String[14] = ('Change record|', 'Save changes|');
begin
State := States[State = sBrowse];
ChangeBtn.Hint := Hints[ChangeBtn.Down]
end;
procedure TDataForm.FirstClick(Sender: TObject);
begin
State := sBrowse;
DataFile.Current := 0;
ShowRecord(DataFile.Current);
end;
procedure TDataForm.PriorClick(Sender: TObject);
begin
State := sBrowse;
DataFile.Current := DataFile.Current - 1;
ShowRecord(DataFile.Current);
end;
procedure TDataForm.NextClick(Sender: TObject);
begin
State := sBrowse;
DataFile.Current := Succ(DataFile.Current);
ShowRecord(DataFile.Current);
end;
procedure TDataForm.LastClick(Sender: TObject);
begin
State := sBrowse;
DataFile.Current := Pred(DataFile.Count);
ShowRecord(DataFile.Current);
end;
procedure TDataForm.ExitClick(Sender: TObject);
begin
Close;
end;
procedure TDataForm.EditItemClick(Sender: TObject);
begin
if ActiveControl is TCustomEdit then
with TCustomEdit(ActiveControl) do
case (Sender as TComponent).Tag of
1: Perform(em_Undo, 0, 0);
2: CutToClipBoard;
3: CopyToClipBoard;
4: PasteFromClipBoard;
5: ClearSelection;
end;
end;
procedure TDataForm.EditBoxKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
SetUpClipBoardItems(Sender as TComponent);
end;
procedure TDataForm.EditBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then
SetUpClipBoardItems(Sender as TComponent);
end;
procedure TDataForm.EditBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
SetUpClipBoardItems(Sender as TComponent);
end;
procedure TDataForm.EditBoxChange(Sender: TObject);
begin
SetUpClipBoardItems(Sender as TComponent);
end;
procedure TDataForm.EditBoxExit(Sender: TObject);
begin
SetUpClipBoardItems(ActiveControl);
end;
initialization
{ Since all labels on the form have had their names removed }
{ this line prevents a startup error - remove it and see }
RegisterClasses([TLabel]);
end.